package com.example.photo_sharing.page.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.photo_sharing.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ImgShowAdapter extends RecyclerView.Adapter<ImgShowAdapter.findViewHolder> {

    private OnItemClickListener onItemClickListener;
    private Context context;
    private ArrayList<String> goodsEntities;

    public ImgShowAdapter(Context context, ArrayList<String> goodsEntities){
        System.out.println("asdasdasdadsasdasd--------------"+goodsEntities.size());

        this.context = context;
        this.goodsEntities = goodsEntities;

    }

    @Override
    public ImgShowAdapter.findViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.list_item_img_show,null);
        return new findViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ImgShowAdapter.findViewHolder holder, int position) {
        String data = goodsEntities.get(position);
        Picasso.get().load(data).into(holder.list_image);



    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }

    class findViewHolder extends RecyclerView.ViewHolder {

        //定义控件
        private ImageView list_image;

        public findViewHolder( View itemView) {
            super(itemView);
            //找到控件
            list_image = itemView.findViewById(R.id.list_img);
            System.out.println("asdasdasdadsasdasd--------------");
            //设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (onItemClickListener!=null){

                        onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    //设置点击事件监听器
    public interface OnItemClickListener {
        public void OnItemClick(View view, String data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

